//
//  ST_EngagementChatBottomToolControl.swift
//  btvideo
//
//  Created by mac on 2020/12/1.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_EngagementChatBottomToolControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.clipsToBounds = true
        
        self.backgroundColor = .clear
        
        self.addSubview(center_btn)
        center_btn.snp.makeConstraints { (make) in
            make.height.equalTo(60+10+20)
            make.width.equalTo(60)
            make.centerX.top.equalToSuperview()
        }
        
        let offset = TIANCompatibleValue(value: 55)
        
        self.addSubview(left_btn)
        left_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(center_btn)
            make.right.equalTo(center_btn.snp.left).offset(-offset)
            make.centerY.equalTo(center_btn)
        }
        
        self.addSubview(right_btn)
        right_btn.snp.makeConstraints { (make) in
            make.left.equalTo(center_btn.snp.right).offset(offset)
            make.centerY.width.height.equalTo(left_btn)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var center_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.setTitleForAllStates("充值".withAttributes(attributes))
        
        control.title_bottom_offset = 0
        
        control.setImageForAllStates(ST_ImageUtils.imageName("chat_bottom_bar_center_icon")!)
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 0, bottom: 30, right: 0)
        
        control.addTarget(self, action: #selector(centerClick), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var attributes = [Attribute.font(UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),Attribute.textColor(.white)]
    
    lazy var left_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.setTitleForAllStates("挂断".withAttributes(attributes))
        
        control.title_bottom_offset = 0
        
        control.setImageForAllStates(ST_ImageUtils.imageName("chat_bottom_bar_left_icon")!)
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 0, bottom: 30, right: 0)
        
        control.addTarget(self, action: #selector(left_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    fileprivate lazy var right_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.setTitleForAllStates("礼物".withAttributes(attributes))
        
        control.title_bottom_offset = 0
        
        control.setImageForAllStates(ST_ImageUtils.imageName("chat_bottom_bar_right_icon")!)
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 0, bottom: 30, right: 0)
        
        control.addTarget(self, action: #selector(right_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    var centerClosure:(()->())?
    @objc fileprivate func centerClick(){
        centerClosure?()
    }
    
    var left_closure:((Bool)->())?
    @objc fileprivate func left_click(){
        
        left_btn.isSelected.toggle()
        
        left_closure?(left_btn.isSelected)
    }
    
    
    var right_closure:((Bool)->())?
    @objc fileprivate func right_click(){
        
        right_btn.isSelected.toggle()
        
        right_closure?(right_btn.isSelected)
    }
    
    func update_left_button(title:String)  {
        
        left_btn.setTitleForAllStates(title.withAttributes(attributes))
        
    }
    
}





//MARK: -

class EngagementChatRightToolControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.clipsToBounds = true
        
        self.backgroundColor = .clear
        
        self.addSubview(topBtn)
        topBtn.snp.makeConstraints { (make) in
            make.height.equalTo(36+5+17)
            make.width.equalTo(40)
            make.centerX.top.equalToSuperview()
        }
        
        let offset = TIANCompatibleValue(value: 20)
        
        self.addSubview(bottomBtn)
        bottomBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(topBtn)
            make.top.equalTo(topBtn.snp.bottom).offset(offset)
            make.centerX.equalTo(topBtn)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var topBtn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.title_bottom_offset = 0
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 2, bottom: 17+5, right: 2)
        
        control.addTarget(self, action: #selector(topClick), for: UIControl.Event.touchUpInside)
        
        control.setImage(ST_ImageUtils.imageName("chat_right_bar_top_icon")!, for: .normal)
        control.setImage(ST_ImageUtils.imageName("chat_right_bar_top_icon_sel")!, for: .selected)
        
        
        control.setTitle("静音关".withAttributes(self.attributes), for: .normal)
        
        control.setTitle("静音开".withAttributes(self.attributes), for: .selected)
        
        return control
    }()
    
    fileprivate lazy var attributes = [Attribute.font(UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),Attribute.textColor(.white)]
    
    fileprivate lazy var bottomBtn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.title_bottom_offset = 0
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 2, bottom: 17+5, right: 2)
        
        control.addTarget(self, action: #selector(bottomClick), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    var topClosure:((Bool)->())?
    @objc fileprivate func topClick(){
        
        topBtn.isSelected.toggle()
        
        topClosure?(topBtn.isSelected)
    }
   
    
    var bottomClosure:((Bool)->())?
    @objc fileprivate func bottomClick(){
        
        bottomBtn.isSelected.toggle()
        
        bottomClosure?(bottomBtn.isSelected)
    }
    
    
    func setBottomBtn(normalTitle:String,selectedTitle:String,normalImage:UIImage,selectedImage:UIImage) {
        
        bottomBtn.setImage(normalImage, for: .normal)
        bottomBtn.setImage(selectedImage, for: .selected)
        
        
        bottomBtn.setTitle(normalTitle.withAttributes(self.attributes), for: .normal)
        
        bottomBtn.setTitle(selectedTitle.withAttributes(self.attributes), for: .selected)
        
        
    }
}
